package com.workerhelping.controller;

import com.workerhelping.config.OSSProperties;
import com.workerhelping.feignClients.MySQLFeignClient;
import com.workerhelping.po.Notice;
import com.workerhelping.result.Result;
import com.workerhelping.result.ResultCodeEnum;
import com.workerhelping.utils.OSSUtil;
import com.workerhelping.vo.SubmitNoticeVO;
import org.aspectj.weaver.ast.Not;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private OSSProperties ossProperties;

    @Autowired
    private MySQLFeignClient mySQLFeignClient;

    /**
     * 1. 文件单独上传
     * 2. oss上传文件返回地址
     * 3. 表单隐藏域存放地址
     *
     * @return
     */
    @PostMapping(value = "/uploadFile")
    public Result<String> uploadFile(@RequestParam(value = "file", required = false) MultipartFile multipartFile) throws IOException {
        if (multipartFile == null || multipartFile.isEmpty())
            return Result.build(null, ResultCodeEnum.FILE_IS_EMPTY);
        return OSSUtil.uploadFileToOss(
                ossProperties.getEndPoint(),
                ossProperties.getAccessKeyId(),
                ossProperties.getAccessKeySecret(),
                multipartFile.getInputStream(),
                ossProperties.getBucketName(),
                ossProperties.getBucketDomain(),
                multipartFile.getOriginalFilename());

    }

    /**
     * 获取政策信息
     * @return
     */
    @GetMapping("/")
    public Result<List<Notice>> search() {
        Result<List<Notice>> result = mySQLFeignClient.search();
        if (!result.isOk())
            return Result.build(null, ResultCodeEnum.DATA_IS_EMPTY);
        List<Notice> notices = result.getData();
        if (notices.isEmpty())
            return Result.build(null, ResultCodeEnum.DATA_IS_EMPTY);
        return Result.ok(notices);
    }

    /**
     * 添加政策信息
     *
     * @param noticeVO
     * @return
     */
    @PostMapping("/add")
    public Result<String> addNotice(@RequestBody SubmitNoticeVO noticeVO) {
        Result<String> addResult = mySQLFeignClient.add(noticeVO);
        if (!addResult.isOk())
            return Result.build(null, ResultCodeEnum.UPLOAD_FAIL);
        return Result.ok();
    }

    /**
     * 根据id查看
     *
     * @param id
     * @return
     */
    @GetMapping("/preview/{id}")
    public Result<Notice> preview(@PathVariable("id") Integer id) {
        Result<Notice> getResult = mySQLFeignClient.getNoticeById(id);
        Notice notice = getResult.getData();
        if (!getResult.isOk())
            return Result.fail();
        return Result.ok(notice);
    }

    /**
     * 根据id删除档案
     *
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public Result<String> delete(@PathVariable("id") Integer id) {
        Result<String> deleteResult = mySQLFeignClient.deleteNoticeById(id);
        if (!deleteResult.isOk())
            return Result.fail();
        return Result.ok();
    }

    /**
     * 查询档案
     *
     * @param keyword
     * @return
     */
    @PostMapping(value = {"/search/{keyword}", "/search"})
    public Result<List<Notice>> search(@PathVariable(value = "keyword", required = false) String keyword) {
        Result<List<Notice>> noticesResults;
        if (StringUtils.isEmpty(keyword))
            noticesResults = mySQLFeignClient.search();
        else noticesResults = mySQLFeignClient.searchByKeyword(keyword);
        if (!noticesResults.isOk())
            return Result.build(null, ResultCodeEnum.SEARCH_FAIL);
        List<Notice> notices = noticesResults.getData();
        if (notices.isEmpty())
            return Result.build(null, ResultCodeEnum.DATA_IS_EMPTY);
        return Result.ok(notices);
    }


}
